package com.examiner.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/*
* 数据库密码加密
* */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 配置认证信息
        auth.inMemoryAuthentication()
                .withUser("jack").password("$2a$10$0RMVBVZYgWy.vvB5.qPu4.3gKRZzJpqZ73LmvN2Z7S1akwQCBWQ0a").roles("admin")
                .and()
                .withUser("user").password("$2a$10$0RMVBVZYgWy.vvB5.qPu4.3gKRZzJpqZ73LmvN2Z7S1akwQCBWQ0a").roles("user");
    }

    /*
    * 配置所有请求都不经过spring security
    * */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable();
    }


    /* 只是用加密单例  */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
